<?php
$sid = (isset($_GET["sid"]))?$_GET["sid"]:-1;
$sids = $_SESSION["ArraySec"];
if($_SESSION['Permit']==1){
	$sql = "select id,name from tblsection";
}else{
	$sql = "select id,name from tblsection where id in ($sids)";
}
$sections = mysql_query($sql);
$tb="tblarticle";
$start=(isset($_GET["st"]))?$_GET["st"]:0;
$limit=100;
if($sid ==-1){
	if($_SESSION['Permit']==1)
 		$sql = "Select $tb.id,$tb.created_date,$tb.image,$tb.tag,$tb.vi_title,$tb.en_title,$tb.published,tbluser.username as username,tblsection.name as sname FROM $tb Inner join tblsection on $tb.section_id  = tblsection.id Inner join tbluser on $tb.user_id=tbluser.id ";
	else
		$sql = "Select $tb.id,$tb.created_date,$tb.image,$tb.tag,$tb.vi_title,$tb.en_title,$tb.published,tbluser.username as username,tblsection.name as sname FROM $tb Inner join tblsection on $tb.section_id  = tblsection.id Inner join tbluser on $tb.user_id=tbluser.id WHERE $tb.section_id in ($sids)";
}
else
{
	$sql = "Select $tb.id,$tb.created_date,$tb.image,$tb.tag,$tb.vi_title,$tb.en_title,$tb.published,tbluser.username as username,tblsection.name as sname FROM $tb Inner join tblsection on $tb.section_id  = tblsection.id Inner join tbluser on $tb.user_id=tbluser.id WHERE $tb.section_id = $sid";
    }
$CountSQL= mysql_num_rows(mysql_query($sql));
$sql.="";
$rs=mysql_query($sql);
?>
<script language="javascript" type="text/javascript">
function delArticle(aid){
	if(confirm("Bạn có chắc chắn muốn xóa bài viết này?")){
		location="delarticle?aid="+aid;
	}
}
function slcSection_change(){
	var sid = document.getElementById("slcSection").value;
	var url = location.href;
	if(url.indexOf("?")!=-1){
		
		url= url.substring(0,url.indexOf("?"));
	}
	if(sid!=0){
		url+="?sid="+sid;
	}
	location.href= url;
}
</script>
<div align="center" style="margin:0px auto">
  <table cellpadding="3" width="740" border="0" align="center">
  
  <tr>
    <td colspan="8"><h3>Danh sách bài viết</h3></td>
  </tr>
  <tr>
    <td height="37" colspan="7">
    	<label>Chuyên mục</label>
    	<select id="slcSection" onchange="slcSection_change();">
    		<option value="0">Tất cả các chuyên mục</option>
    	<?php
    	 while($section=mysql_fetch_object($sections)){
    	 	if($section->id == $sid){
    	 		echo "<option selected = 'selected' value='$section->id'>$section->name</option>";
    	 	}else{
    	 		echo "<option value='$section->id'>$section->name</option>";
    	 	}
    	 }
    	?>
    	</select>
	</td>
    <td align="right">
    	<a  href="addarticle" title="Tạo bài viết">
    		<img src="img/Add.png" style="border:none;" width="32" height="32"/>
    	</a>
    </td>
  </tr>
  <tr bgcolor="#008000" style="color:white; height:30px">
    <th align="center" width="5%">STT</th>
    <th width="20%"><strong>Tiêu đề Tiếng Việt</strong></th>
    <th width="20%"><strong>Tiêu đề Tiếng Anh</strong></th>
    <th width="10%"><strong>Chuyên mục</strong></th>
    <th width="10%"><strong>Người tạo</strong></th>
    <th width="10%"><strong>Ngày tạo</strong></th>
    <th width="10%"><strong>Published</strong></th>
    <th width="15%"><strong>Thực hiện</strong></th>
  </tr>
  <?php	
  	$i=1;
	while($row=mysql_fetch_object($rs))
	{
			
  ?>
  <tr style="background:#e7e7e7;">
    <td align="center"><?php echo $i++;?></td>
    <td><?php echo $row->vi_title ?></td>
    <td><?php echo $row->en_title ?></td>
    <td><?php echo $row->sname?></td>
    <td><?php echo $row->username?></td>
    <td><?php echo $row->created_date?></td>
    <td><?php echo ($row->published==1)?"published":"unpublished"?></td>
   
    <td>
    	<a href="editarticle?aid=<?php echo $row->id?>">Edit/Detail</a>&nbsp;|&nbsp;<a href="" onclick="delArticle(<?php echo $row->id?>);return false;">Delete</a></td>
  </tr>
  <?php

   }
  ?>
  
  </table>
  <table cellpadding="3" width="740" border="0" align="left">
  <tr>
  <?php $totalpage=($CountSQL/10);
  settype($totalpage,"integer");
  if(($CountSQL%10)!=0)$totalpage++;
 
  for($i=0;$i<$totalpage;$i++)
{ 
	echo ("<td><a href='article?sid=".$_GET['sid']."&st=".($i)."'>".($i+1)."</a></td>");
	}  
   ?>
  </tr>
  </table>
</div>
